package com.sgu6.sgu6_machine.ui.fragment.buy;


import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.sgu6.middleware.util.HexUtil;
import com.sgu6.serialport.manager.SerialPortManagerForCard;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.container.ComContainer;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.event.ReadCardEvent;
import com.wang.avi.AVLoadingIndicatorView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 提示刷卡
 */
public class SwipeCardF extends BaseF {

    @BindView(R.id.ivSwipeCard)
    ImageView ivSwipeCard;
    @BindView(R.id.loading)
    AVLoadingIndicatorView loading;
    @BindView(R.id.notice)
    TextView notice;
    private String cardNo = "";

    public static Fragment newInstance(String params) {
        return new SwipeCardF();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView: ");
        View view = inflater.inflate(R.layout.fragment_swipe_card, container, false);

        unbinder = ButterKnife.bind(this, view);

        return view;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.i(TAG, "onHiddenChanged: "+hidden);
        if (!hidden){
            loading.setVisibility(View.GONE);
            ivSwipeCard.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
//        loading.setVisibility(View.GONE);
//        ivSwipeCard.setVisibility(View.VISIBLE);
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
        loading.setVisibility(View.GONE);
        ivSwipeCard.setVisibility(View.VISIBLE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReadCardEvent(ReadCardEvent event) {
        if (event.cmd.length() == 40 && MachineContainer.isConsumption) {
            loading.setVisibility(View.VISIBLE);
            ivSwipeCard.setVisibility(View.GONE);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "onDestroyView: ");
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
        EventBus.getDefault().unregister(this);
    }
}
